#include <nxos.h>
#include <PowerBox.h>

NX_Error PowerBoxBuiltinSleep(int argc, char *argv[], char * env)
{
    NX_U32 ms;
    NX_U32 times = 1;
    
    if (argc < 2)
    {
        NX_Printf("Usage:\n  sleep [ms] [times]\n");
        return NX_EAGAIN;
    }

    ms = NX_StrToUL(argv[1], NX_NULL, 10);

    if (!ms)
    {
        ms = 1000;
    }

    if (argc == 3)
    {
        times = NX_StrToUL(argv[2], NX_NULL, 10);
        if (!times)
        {
            times = 1;
        }
    }

    NX_Printf("sleep %d ms %d times\n", ms, times);

    while (times > 0)
    {
        if (times > 0) {
            NX_Printf(".");
        }
        NX_ThreadSleep(ms);
        times--;
    }

    return NX_EOK;
}
